/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.matapelajaran;

import com.ivanbiz.injection.CallCtx;
import com.ivanbiz.properties.SISBeanConstants;
import com.ivanbiz.properties.SISConstants;
import com.ivanbiz.properties.SISScreenConstants;
import com.ivanbiz.tools.IvanBiz;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Ivan
 */
public class MataPelajaranAction extends IvanBiz {

    ActionMessages actionMessages = new ActionMessages();

    public ActionForward mataPelajaranList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        MataPelajaranService mataPelajaranService = (MataPelajaranService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_SERVICE);
        try {
            List listMataPelajaran = mataPelajaranService.loadAll();
            request.setAttribute(SISConstants.LIST_MATA_PELAJARAN, listMataPelajaran);
        } catch (Exception ex) {
            Logger.getLogger(MataPelajaranAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.MATA_PELAJARAN_LIST);
    }

    public ActionForward mataPelajaranAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        MataPelajaranService mataPelajaranService = (MataPelajaranService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_SERVICE);
        MataPelajaranForm mataPelajaranForm = (MataPelajaranForm) form;
        try {
            boolean kodeMataPelajaranIsUsed = mataPelajaranService.cekIsEmptyKode(mataPelajaranForm.getKode());
            if (kodeMataPelajaranIsUsed) {
                actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.CODE_IS_USED));
                saveMessages(request, actionMessages);
                return mapping.findForward(SISScreenConstants.MATA_PELAJARAN_LIST);
            } else {
                MataPelajaran mataPelajaran = new MataPelajaran();
                mataPelajaran.setKode(mataPelajaranForm.getKode());
                mataPelajaran.setNama(mataPelajaranForm.getNama());
                mataPelajaranService.saveMataPelajaran(mataPelajaran);
                mataPelajaranForm.reset(mapping, request);
            }
        } catch (Exception ex) {
            Logger.getLogger(MataPelajaranAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.MATA_PELAJARAN);
    }

    public ActionForward mataPelajaranEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        MataPelajaranService mataPelajaranService = (MataPelajaranService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_SERVICE);
        MataPelajaranForm mataPelajaranForm = (MataPelajaranForm) form;
        try {
            MataPelajaran mataPelajaran = mataPelajaranService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            mataPelajaranForm.setKode(mataPelajaran.getKode());
            mataPelajaranForm.setNama(mataPelajaran.getNama());
        } catch (Exception ex) {
            Logger.getLogger(MataPelajaranAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.MATA_PELAJARAN_EDIT);
    }

    public ActionForward mataPelajaranUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        MataPelajaranService mataPelajaranService = (MataPelajaranService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_SERVICE);
        MataPelajaranForm mataPelajaranForm = (MataPelajaranForm) form;
        try {
            boolean kodeMataPelajaranIsUsed = mataPelajaranService.cekIsEmptyKode(mataPelajaranForm.getKode());
            if (kodeMataPelajaranIsUsed) {
                actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.CODE_IS_USED));
                saveMessages(request, actionMessages);
                return mapping.findForward(SISScreenConstants.MATA_PELAJARAN_EDIT);
            } else {
                MataPelajaran mataPelajaran = mataPelajaranService.loadById(mataPelajaranForm.getId());
                mataPelajaran.setKode(mataPelajaranForm.getKode());
                mataPelajaran.setNama(mataPelajaranForm.getNama());
                mataPelajaranService.updateMataPelajaran(mataPelajaran);
                mataPelajaranForm.reset(mapping, request);
            }
        } catch (Exception ex) {
            Logger.getLogger(MataPelajaranAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.findForward(SISScreenConstants.MATA_PELAJARAN_EDIT));
        }
        return mapping.findForward(SISScreenConstants.MATA_PELAJARAN);
    }

    public ActionForward mataPelajaranDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        MataPelajaranService mataPelajaranService = (MataPelajaranService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_SERVICE);
        MataPelajaranForm mataPelajaranForm = (MataPelajaranForm) form;
        try {
            MataPelajaran mataPelajaran = mataPelajaranService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            mataPelajaranService.deleteMataPelajaran(mataPelajaran);
        } catch (Exception ex) {
            Logger.getLogger(MataPelajaranAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.MATA_PELAJARAN);
    }
}